package com.yhb.service.impl;

import com.yhb.dao.MessageMapper;
import com.yhb.model.Message;
import com.yhb.service.MessageService;
import me.edzh.spring.bootstrap.service.PushNotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.util.List;

@EnableAsync
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private PushNotificationService pushNotificationService;

    @Override
    @Async
    public void addMessage(Message message, boolean shouldPush) {

        messageMapper.add(message);

        if (shouldPush) {
            pushNotificationService.send(message.getUserId(), message.getTitle(), null, PushNotificationService.PUSH_ALL);
        }
    }

    @Override
    public List<Message> userMessages(String userId, Message.Type type) {
        return messageMapper.userMessagesOfType(userId, type);

    }

    @Override
    public List<Message> userFirstMessages(String userId, Message.Type type) {
        return messageMapper.userFirstMessagesOfType(userId, type);
    }
}
